<?php

/**
 * LevelStudyKit filter form.
 *
 * @package    elearning
 * @subpackage filter
 * @author     Your name here
 */
class LevelStudyKitFormFilter extends BaseLevelStudyKitFormFilter
{
  public function configure()
  {
	$this->widgetSchema['lsk_status'] = new sfWidgetFormChoice(array(
			'choices' => TestPeer::getTestStatusChoices(),
			'expanded' => true,
		));
		
	$this->validatorSchema['lsk_status'] = new sfValidatorChoice(array(
					'required'      => false,
					'choices'       => array_keys(TestPeer::getTestStatusChoices()),
			));
	//$this->setDefaults( array('lsk_status' => null ) );
 	//display
  	$this->widgetSchema['lvName'] = new sfWidgetFormFilterInput();
	$this->validatorSchema['lvName'] = new sfValidatorPass(array('required' => false));
	$this->widgetSchema->setNameFormat('level_filters[%s]');
  }
  
  //1.0 Fillter name
  public function getFields()
  {
    return array(
      'id'         => 'Number',
      'level_id'   => 'ForeignKey',
      'lsk_image'  => 'Text',
      'lsk_link'   => 'Text',
      'lsk_status' => 'ForeignKey',
      'created_at' => 'Date',
      'updated_at' => 'Date',
	  'lvName'     => 'Text',
    );
  }
  //1.1 do ham getValue khong lay duoc gia tri gi, nen danh phai chien bang cach tach lay chuoi html ma get value
  public static function getValueOf($string)
  {
  	$find = "value=";
  	if( isset($string) == false )
		return false;
	$pos = stripos( $string, $find );
	if ( $pos == false )
		return false;
	//tim dau ["] mo
	$pos1 = stripos( $string, '"' , $pos);
	// khong thay dau ["] sau cum value=
	if ( $pos1 == false )
		return false;
	$pos2 = stripos( $string, '"' , $pos1 + 1);
	//Khong thay dau ["] dong ket thuc
	if ( $pos2 == false )
		return false;
	if ( $pos1 + 1 == $pos2 )
		return false;
	$value = substr ($string, $pos1 + 1, $pos2 -  $pos1 - 1 );
	return $value;
  }
  //1.2 Lay du lieu tim duoc va add no vao query de filter
  public function addLvNameColumnCriteria(Criteria $c, $field, $value) {
		$value = $this::getValueOf($this[$field]);
		if($value == false || $value == "")
		{
			//dosmt
		}
		else	
			//$this[$field]->getValue()
			$c->add(LevelStudyKitI18nPeer::LSK_NAME, "%" . strtr($value, '*', '%') . "%", Criteria::LIKE);
		$level_id = $this['level_id']->getValue();
		if( isset($level_id) )
			$c->add(LevelStudyKitPeer::LEVEL_ID, $this['level_id']->getValue(), Criteria::EQUAL);
		//parent::addFiltersCriteria($c);
		return $c;
	}
}
